package com.djl.sqtg.controller;

import com.djl.sqtg.entity.LayuiResultData;
import com.djl.sqtg.entity.Review;
import com.djl.sqtg.service.IOrderService;
import com.djl.sqtg.service.IReviewService;
import com.djl.sqtg.service.IUserService;
import com.djl.sqtg.util.DateUtil;
import com.djl.sqtg.util.JsonResult;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("review")
public class ReviewController extends BaseController{
    @Autowired
    private IReviewService reviewService;
    @Autowired
    private IOrderService orderService;

/*    @GetMapping({"getAllReview"})
    public LayuiResultData getAllReview(){
        List<Review> reviews = reviewService.getAllReview();
        LayuiResultData data = new LayuiResultData();
        data.setData(reviews);
        data.setCount(reviews.size());
        return data;
    }*/

    @GetMapping({"getAllReview"})
    public LayuiResultData getAllReview(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize){
        System.out.println(pageNum+","+pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<Review> reviews = reviewService.getAllReview();
        Integer count=reviewService.getAllNum();
        LayuiResultData data = new LayuiResultData();
        data.setData(reviews);
        data.setCount(count);
        data.setPageNum(pageNum);
        data.setPageSize(pageSize);
        System.out.println(data);
        return data;
    }


    @GetMapping({"updateReviewState/{rid}/{state}"})
    public JsonResult updateReviewState(@PathVariable Integer rid,@PathVariable Integer state){
        reviewService.updateReviewState(rid,state);
        return new JsonResult(OK);
    }

    @PostMapping({"addReview"})
    public JsonResult addReview(@RequestBody Review review){
        review.setReviewtime(DateUtil.getNowTime());
        System.out.println(review.toString());
        reviewService.addReview(review);
        //修改该条订单的状态为3
        orderService.updateOrderState(review.getOid(),3);
        return new JsonResult(OK);
    }
/*若前端使用ajax的formdata时，后台接收数据时不使用任何注解*/
    @PostMapping({"replyReview"})
    public JsonResult replyReview(Review review){
        System.out.println(review.toString());
        reviewService.replyReview(review.getRid(), review.getReply());
        return new JsonResult(OK);
    }

    @GetMapping({"delReview/{rid}"})
    public JsonResult delReview(@PathVariable Integer rid){
        reviewService.delReview(rid);
        return new JsonResult(OK);
    }

}
